"""
@Author  : 康帅
@Time    : 2021/3/12 11:05
@Function: 中介模式:又称调停模式(租房案例)
"""

"""
中介模式好处：
在很多系统中，多个类很容易相互耦合，形成网状结构。
中介模式的作用就是将这种网状结构（1-1）分离成星型结构（1-2）。
这样调整之后，使得对象间的结构更加简洁，交互更加顺畅
"""


class HouseInfo:
    """ 租赁房屋展示 """

    def __init__(self, area, price, has_window, has_bathroom, has_kitchen, address, owner):
        self.__area = area
        self.__price = price
        self.__has_window = has_window
        self.__has_bathroom = has_bathroom
        self.__has_kitchen = has_kitchen
        self.__address = address
        self.__owner = owner

    def get_address(self):
        """ 获取地址 """

        return self.__address

    def get_owner_name(self):
        """ 获取房东姓名 """

        return self.__owner.get_name()

    def show_info(self, is_show_owner=True):
        """ 房屋信息展示 """

        print("面积：" + str(self.__area) + "平方米",
              "价格：" + str(self.__price) + "元",
              "窗户：" + ("有" if self.__has_window else "没有"),
              "卫生间：" + self.__has_bathroom,
              "厨房：" + ("有" if self.__has_kitchen else "没有"),
              "地址：" + self.__address,
              "房东：" + self.get_owner_name() if is_show_owner else "")


class HousingAgency:
    """ 房屋中介 """

    def __init__(self, name):
        self.__house_infos = []
        self.__name = name

    def get_name(self):

        return self.__name

    def add_house_info(self, house_info):
        """ 添加房源信息 """

        self.__house_infos.append(house_info)

    def remove_house_info(self, house_info):
        """ 移除房源信息 """

        for info in self.__house_infos:
            if info == house_info:
                self.__house_infos.remove(info)

    @classmethod
    def get_search_condition(cls, description):
        """ 这里有一个用户描述信息转换成搜索条件的逻辑 """

        return description

    def get_match_info(self, search_condition):
        """ 根据房源信息的各个属性查找最匹配的信息 """

        print(self.get_name(), "为您找到以下合适的房源：")
        for info in self.__house_infos:
            info.show_info(False)
        return self.__house_infos

    def sign_contract(self, house_info, period):
        """ 与房东签的协议 """

        print(self.get_name(), "与房东", house_info.get_owner_name(), "签订", house_info.get_address(),
              "的房子的租赁合同，租期", period, "年。合同期内", self.get_name(), "有权对其进行使用和转租！")

    def sign_contracts(self, period):
        for info in self.__house_infos:
            self.sign_contract(info, period)


class HouseOwner:
    """ 房东 """

    def __init__(self, name):
        self.__name = name
        self.__house_info = None

    def get_name(self):

        return self.__name

    def set_house_info(self, address, area, price, has_window, has_bathroom, has_kitchen):
        """ 设置房屋展示信息 """

        self.__house_info = HouseInfo(area, price, has_window, has_bathroom, has_kitchen, address, self)

    def publish_house_info(self, agency):
        """ 发布房源出租信息 """

        agency.add_house_info(self.__house_info)
        print(self.get_name() + "在", agency.get_name(), "发布房源出租信息：")
        self.__house_info.show_info()


class Customer:
    """ 租户 """

    def __init__(self, name):
        self.__name = name

    def get_name(self):
        return self.__name

    def find_house(self, description, agency):
        """ 找房 """

        print("我是" + self.get_name() + ", 我想要一个\"" + description + "\"的房子")
        print()
        return agency.get_match_info(agency.get_search_condition(description))

    def see_house(self, house_infos):
        """ 去看房 """

        size = len(house_infos)
        return house_infos[size - 1]

    def sign_contract(self, house_info, agency, period):
        """ 与中介签订协议 """

        print(self.get_name(), "与中介", agency.get_name(), "签订", house_info.get_address(),
              "的房子的租赁合同，租期", period, "年。合同期内", self.__name, "有权对其进行使用!")


if __name__ == '__main__':
    my_home = HousingAgency("我爱我家")
    zhangsan = HouseOwner("张三")
    zhangsan.set_house_info("上地西里", 20, 2500, 1, "独立卫生间", 0)
    zhangsan.publish_house_info(my_home)
    lisi = HouseOwner("李四")
    lisi.set_house_info("当代城市家园", 16, 1800, 1, "公用卫生间", 0)
    lisi.publish_house_info(my_home)
    wangwu = HouseOwner("王五")
    wangwu.set_house_info("新都汇", 18, 2600, 1, "独立卫生间", 1)
    wangwu.publish_house_info(my_home)
    print()

    my_home.sign_contracts(3)
    print()

    tony = Customer("Tony")
    house_infos = tony.find_house("18 平方米左右,要有独立卫生间,要有窗户,最好朝南,有厨房更好！价格在2000元左右", my_home)
    print()
    print("正在看房，需找合适的住巢")
    print()
    appropriate_house = tony.see_house(house_infos)
    tony.sign_contract(appropriate_house, my_home, 1)
